import { useReducer } from 'react';

export interface IRoleManageTypeState {
  searchTableValue: string; //搜索框输入的值
  expandedRowKeys: string[]; //选择表格数据的展开值
  // autoExpandParent: boolean;
  checkedRowKeys: string[];
  // roleCurrentIndex: number;  //步数变化的 下标
  // roleCurrentKey: string  //步数变化的key
  detailMenuLeftValue: string;
  roleDetailRightValue: string; //详情菜单右侧搜索值
}

interface IActionType {
  type:
    | 'role-remove-allState'
    | 'role-searchTableValue'
    | 'role-expandedRowKeys'
    | 'role-checkedRowKeys'
    | 'role-detailMenuLeftValue'
    | 'role-roleDetailRightValue'
    // | 'role-roleCurrentIndex'
    | 'role-roleCurrentKey';
  payload?: any;
}

export const useRoleManageReducer = () => {
  const initState: IRoleManageTypeState = {
    //初始值
    expandedRowKeys: [],
    searchTableValue: '',
    checkedRowKeys: [],
    detailMenuLeftValue: '',
    roleDetailRightValue: '',
    // roleCurrentIndex: 0,
    // roleCurrentKey: 'setMenu'
  };

  const roleReducer = (state: IRoleManageTypeState, actions: IActionType) => {
    switch (actions?.type) {
      case 'role-roleDetailRightValue':
        return {
          ...state,
          roleDetailRightValue: actions?.payload,
        };

      case 'role-detailMenuLeftValue':
        return {
          ...state,
          detailMenuLeftValue: actions?.payload,
        };

      case 'role-searchTableValue':
        return {
          ...state,
          searchTableValue: actions?.payload,
        };
      case 'role-expandedRowKeys':
        return {
          ...state,
          expandedRowKeys: actions?.payload,
        };
      case 'role-checkedRowKeys': //表格选中项key
        return {
          ...state,
          checkedRowKeys: actions?.payload,
        };
      // case 'role-roleCurrentIndex':
      //   return {
      //     ...state,
      //     roleCurrentIndex: actions?.payload,
      //   }
      // case 'role-roleCurrentKey':
      //   return {
      //     ...state,
      //     roleCurrentKey: actions?.payload,
      //   }
      case 'role-remove-allState': {
        return {
          ...state,
          ...initState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };

  const [state, rouleDispatch] = useReducer(roleReducer, initState);

  return {
    ...state,
    rouleDispatch,
  };
};
